/**
 * @date 2022/4/1
 * @description vite.config.js配置文件
 * @author 
 */

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path')
const isDev = process.env.NODE_ENV === 'development'
const isMock = process.env.NODE_ENV === 'mock'
import { setting } from './src/config/setting'

const localEnabled = process.env.USE_MOCK || false
const prodEnabled = process.env.USE_CHUNK_MOCK || false

// vite打包后的文件提供传统浏览器兼容性支持
import legacy from '@vitejs/plugin-legacy'

//mock模拟数据
import { viteMockServe } from 'vite-plugin-mock'

//vite项目组件库按需引入
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'


const { base, publicDir, outDir, assetsDir, sourcemap, cssCodeSplit, host, port, strictPort, open, cors, brotliSize, logLevel, clearScreen, drop_console, drop_debugger, chunkSizeWarningLimit } = setting

const getViteEnv = (mode, target) => {
    return loadEnv(mode, process.cwd())[target]
}
export default defineConfig(({ mode }) => {
    // 获取当前环境的配置
    // const config = loadEnv(mode, "./");
    const baseUrl = loadEnv(mode, process.cwd()).VITE_BASIC_URL
    return {
        base,
        root: process.cwd(),
        publicDir,
        logLevel,
        clearScreen,
        plugins: [
            vue(),
            legacy({
                polyfills: ['es.promise.finally', 'es/map', 'es/set'],
                modernPolyfills: ['es.promise.finally'],
            }),
            // viteMockServe({
            //     mockPath: "mock", //mock文件地址
            //     supportTs: false, //打开后，可以读取 ts 文件模块。 请注意，打开后将无法监视.js 文件
            //     localEnabled: localEnabled, // 开发打包开关
            //     prodEnabled: prodEnabled, // 生产打包开关
            //     injectCode: `
            //      import { setupProdMockServer } from './mockProdServer';
            //      setupProdMockServer();
            //    `,
            // }),
            Components({
                resolvers: [
                    AntDesignVueResolver(),
                    ElementPlusResolver({
                        importStyle: 'sass',
                    }),
                ],
            }),
        ],
        server: {
            host,
            port,
            cors,
            strictPort,
            open,
            fs: {
                strict: false,
            },
            proxy: {
                //设置反向代理
                '/api': {
                    // 后台地址
                    target: baseUrl,
                    ws: true,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, ''),
                },
            },
        },
        resolve: {
            // 设置别名
            alias: {
                views: path.resolve(__dirname, 'src/views'),
                styles: path.resolve(__dirname, 'src/styles'),
                '@': path.resolve(__dirname, 'src'),
            },
        },
        css: {
            preprocessorOptions: {
                // 引入公用的样式
                scss: {
                    additionalData: `@use "@/styles/index.scss" as *;`,
                },
            },
        },
        corePlugins: {
            preflight: false,
        },
        build: {
            target: 'es2015',
            outDir,
            assetsDir,
            sourcemap,
            cssCodeSplit,
            brotliSize,
            terserOptions: {
                compress: {
                    keep_infinity: true,
                    // 用于删除生产环境中的console
                    drop_console,
                    drop_debugger,
                },
            },
            chunkSizeWarningLimit,
        },
        optimizeDeps: {
            // 检测需要预构建的依赖项
            entries: [],
        },
    }
})